{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "id": "6C60C414F3F64381A8E0E18793D0FF2B",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import random\n",
    "import datetime\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "id": "1D17473B466E4EE99939377F6C7A0FE8",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "my_token = '你的 tushare token'\n",
    "import tushare as ts\n",
    "ts.set_token(my_token)\n",
    "pro = ts.pro_api()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "id": "DC09C39F254740478929FA9B0FAD13AA",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "seed = 111\n",
    "#os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"3\"\n",
    "import tensorflow as tf\n",
    "from sklearn.preprocessing import scale\n",
    "#tf.set_random_seed(seed)\n",
    "tf.random.set_seed(seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "id": "A5C2E3084CF54CB194C0C779387A77BA",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "ts_df = pd.read_csv('ts_1e12.csv').set_index(['ts_code'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "id": "7E2EDAFCCD45417C8C42E74B196AEC0F",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "date_label = ts_df.loc['000001.SZ'].set_index(['trade_date']).sort_index()#.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#data = np.random.randn(1,128,6)\n",
    "import requests\n",
    "import json\n",
    "\n",
    "url = 'https://www.heywhale.com/api/model/services/63b136ebcfd9524986b7b4a3?Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyT2lkIjoiNWFkOWU3NjA3MjM4NTE1ZDgwYjc4MDU5IiwiYXBwIjoibW9kZWwiLCJpYXQiOjE2NzI1NTgzNzd9.7O0CquLnwzkQ7c80y_UJkAh7hGVRS08t1LrwPebSNcc'\n",
    "\n",
    "\n",
    "headers = {\n",
    "  'Content-Type': 'application/json'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "3C59A9A3CC074835B67A04700C4FE990",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "def huice(date):\n",
    "    test_input = []\n",
    "    test_output = []\n",
    "    length = 128\n",
    "    test_sate = date\n",
    "    #test_sate = datetime.datetime.strptime(test_sate, \"%Y-%m-%d\")\n",
    "    insts = []\n",
    "    test_cha = []\n",
    "    for inst in list(set(ts_df.index)):\n",
    "        fields = ['close', 'open', 'high', 'low', 'vol','amount']\n",
    "        field1 = ['close', 'open', 'high', 'low']\n",
    "        field2 =  [ 'vol','amount']\n",
    "        date_label = ts_df.loc['000001.SZ'].set_index(['trade_date']).sort_index()\n",
    "        \n",
    "        data = ts_df.loc[inst]#.dropna()\n",
    "        if len(data)<128:\n",
    "            continue\n",
    "    \n",
    "        data  = data.set_index(['trade_date']).sort_index()\n",
    "        data['return_-20'] = (data['close'] - data['close'].shift(20))/data['close'].shift(20)\n",
    "        #data['return_20'] = ( data['close'].shift(-20)- data['close'] )/data['close']\n",
    "        data = data.dropna()\n",
    "        #list_date = data[(data['return_-20']>0) & (data['return_-20']<0.2)& (data['return_20']>0.4)]\n",
    "        #list_date = list_date.index.get_level_values('trade_date').values \n",
    "        s1 = date_label.loc[:test_sate].index[-1]\n",
    "        if len(data.loc[:test_sate])<128:\n",
    "            continue\n",
    "        #print(data.loc[:test_sate].shape)\n",
    "        s2 = data.loc[:test_sate].index[-1]\n",
    "        if s1 > s2:\n",
    "            continue\n",
    "        #print(data.loc[:test_sate].shape)\n",
    "        f1 = scale(data.loc[:test_sate][field1][-length:].values.reshape(1,-1)).reshape(-1,4)\n",
    "        f2 = scale(data.loc[:test_sate][field2][-length:].values)\n",
    "        #print(f2.shape)\n",
    "        #print(data.loc[:test_sate].shape)\n",
    "        if f1.shape[0]<128:\n",
    "            print(inst)\n",
    "        #print(data.loc[:test_sate].shape)\n",
    "        #test_input.append(np.hstack((f1,f2)))\n",
    "        test_input.append(np.array(scale(data.loc[:test_sate][fields][-length:].values)))\n",
    "        #test_output.append(data.loc[:test_sate]['return_20'].iloc[-1])\n",
    "        test_cha.append(data.loc[:test_sate]['return_-20'].iloc[-1])\n",
    "        insts.append(inst)\n",
    "    body = {\n",
    "    \"content\": {\n",
    "        \"input\": {\n",
    "          \"data\": np.array(test_input).tolist()\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    payload = json.dumps(body)\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "\n",
    "    out = json.loads(response.text)['output']\n",
    "    #pred = model.predict(np.array(test_input))\n",
    "    ds =pd.DataFrame()\n",
    "    #ds[ 'tag'] = np.array(test_output)\n",
    "    #ds[ 'cha'] = np.array(test_cha)\n",
    "    ds['m_max'] = tf.nn.softmax(out)[:,1]\n",
    "    ds['code']   = insts\n",
    "    return ds#.sort_values('m_max')['code'].iloc[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "id": "BC72E9F527844EA68027ACBF885FC06A",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "####"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "D672A6DA22044421924584DCEFE45A4F",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "C7FA3E46D9E54A239A1FF8120B1D1F3E",
    "jupyter": {},
    "notebookId": "63aed0abc634be74590687ef",
    "scrolled": false,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "MarkDown菜单",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
